package com.example.wangzh.developkit.utils;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.constant.LogTag;
import com.example.wangzh.developkit.constant.StorageConstant;

import java.util.ArrayList;
import java.util.List;

import me.phelps.library.PopZoomGallery;
import me.phelps.library.ZoomGalleryAdapter;
import me.phelps.library.ZoomImageModel;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by wangzh on 2017/11/10.
 * 展示大图
 */

public class GallaryUtil {
    /**
     * 重载根方法
     *
     * @param activity
     * @param imgUrls
     * @param hasBottom
     */
    public static void showBigPic(final Activity activity, List<String> imgUrls, boolean hasBottom,
                                  int position) {
        ArrayList<ZoomImageModel> zoomImageArrayList = new ArrayList<>();
        for (int i = 0; i < imgUrls.size(); i++) {

            ZoomImageModel imageScale = new ZoomImageModel();
            imageScale.rect = new Rect();
            imageScale.smallImagePath = imgUrls.get(i);
            imageScale.bigImagePath = imgUrls.get(i);
            zoomImageArrayList.add(imageScale);
        }
        initPopZoomGallery(activity, zoomImageArrayList, hasBottom, position);
    }


    public static void showBigPic(final Activity activity, List<String> imgUrls) {
        showBigPic(activity, imgUrls, false, 0);
    }

    public static void showBigPic(final Activity activity, String imgUrl, boolean hasBottom) {
        ArrayList<String> imgUrls = new ArrayList<>();
        imgUrls.add(imgUrl);
        showBigPic(activity, imgUrls, hasBottom, 0);
    }

    public static void showBigPic(final Activity activity, String imgUrl) {
        showBigPic(activity, imgUrl, false);
    }

    private static void initPopZoomGallery(final Activity activity,
                                           ArrayList<ZoomImageModel> zoomImageArrayList,
                                           boolean hasBottom, int position) {
        PopZoomGallery popZoomGallery = new PopZoomGallery(activity, zoomImageArrayList,
                new ZoomGalleryAdapter.ZoomGalleryInstantiateItem() {
                    @Override
                    public void onItemInstantiate(ViewGroup container, int position, PhotoView view,
                                                  final ImageView img_loading, ZoomImageModel model) {
                        img_loading.setVisibility(View.VISIBLE);
                        final ObjectAnimator anim = ObjectAnimator.ofInt(img_loading, "ImageLevel", 0, 10000);
                        anim.setDuration(2000);
                        anim.setRepeatCount(ObjectAnimator.INFINITE);
                        anim.start();

                        Glide.with(activity)
                                .load(model.bigImagePath).error(R.drawable.blankness_small)
                                .crossFade()
                                .listener(new RequestListener<String, GlideDrawable>() {
                                    @Override
                                    public boolean onException(Exception e, String model,
                                                               Target<GlideDrawable> target,
                                                               boolean isFirstResource) {
                                        anim.cancel();
                                        img_loading.setVisibility(View.GONE);
                                        LogUtils.i(LogTag.TEST, "onException: ");
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(GlideDrawable resource,
                                                                   String model,
                                                                   Target<GlideDrawable> target,
                                                                   boolean isFromMemoryCache,
                                                                   boolean isFirstResource) {
                                        anim.cancel();
                                        img_loading.setVisibility(View.GONE);
                                        LogUtils.i(LogTag.TEST, "onResourceReady: ");
                                        return false;
                                    }
                                })
                                .into(view);
                    }

                });
        if (hasBottom) {
            popZoomGallery.setHasBottom(hasBottom);
            popZoomGallery.setSaveDir(StorageConstant.STORAGE_DIR + "download/");
        }
        popZoomGallery.showPop(activity.getWindow().getDecorView(), position);
    }
}
